package org.example.lethear.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.lethear.dto.common.PageResult;
import org.example.lethear.dto.user.UserDTO;
import org.example.lethear.dto.user.UserUpdateRequest;
import org.example.lethear.service.UserService;
import org.example.lethear.utils.Result;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制器
 */
@Tag(name = "用户管理", description = "用户信息管理、关注功能、用户查询等相关接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {
    
    private final UserService userService;
    
    /**
     * 获取当前用户信息
     */
    @Operation(summary = "获取当前用户信息", description = "获取当前登录用户的详细信息")
    @GetMapping("/me")
    public Result<UserDTO> getCurrentUser() {
        UserDTO user = userService.getCurrentUser();
        return Result.success("获取成功", user);
    }
    
    /**
     * 更新用户信息
     */
    @Operation(summary = "更新用户信息", description = "更新当前用户的个人信息")
    @PutMapping("/me")
    public Result<UserDTO> updateUser(@Valid @RequestBody UserUpdateRequest request) {
        UserDTO user = userService.updateUser(request);
        return Result.success("更新成功", user);
    }
    
    /**
     * 根据用户ID获取用户信息
     */
    @Operation(summary = "根据ID获取用户信息", description = "获取指定用户的公开信息")
    @GetMapping("/{userId}")
    public Result<UserDTO> getUserById(@Parameter(description = "用户ID") @PathVariable Long userId) {
        UserDTO user = userService.getUserById(userId);
        return Result.success("获取成功", user);
    }
    
    /**
     * 根据用户名获取用户信息
     */
    @Operation(summary = "根据用户名获取用户信息", description = "获取指定用户名的公开信息")
    @GetMapping("/username/{username}")
    public Result<UserDTO> getUserByUsername(@Parameter(description = "用户名") @PathVariable String username) {
        UserDTO user = userService.getUserByUsername(username);
        return Result.success("获取成功", user);
    }
    
    /**
     * 关注用户
     */
    @Operation(summary = "关注用户", description = "关注指定用户")
    @PostMapping("/{userId}/follow")
    public Result<Void> followUser(@Parameter(description = "用户ID") @PathVariable Long userId) {
        userService.followUser(userId);
        return Result.success("关注成功");
    }
    
    /**
     * 取消关注用户
     */
    @Operation(summary = "取消关注用户", description = "取消关注指定用户")
    @DeleteMapping("/{userId}/follow")
    public Result<Void> unfollowUser(@Parameter(description = "用户ID") @PathVariable Long userId) {
        userService.unfollowUser(userId);
        return Result.success("取消关注成功");
    }
    
    /**
     * 检查是否关注了指定用户
     */
    @Operation(summary = "检查关注状态", description = "检查当前用户是否关注了指定用户")
    @GetMapping("/{userId}/follow-status")
    public Result<Boolean> isFollowing(@Parameter(description = "用户ID") @PathVariable Long userId) {
        boolean isFollowing = userService.isFollowing(userId);
        return Result.success("查询成功", isFollowing);
    }
    
    /**
     * 获取用户的关注列表
     */
    @Operation(summary = "获取关注列表", description = "获取用户关注的人员列表")
    @GetMapping("/{userId}/following")
    public Result<PageResult<UserDTO>> getFollowing(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size) {
        PageResult<UserDTO> users = userService.getFollowing(userId, page, size);
        return Result.success("获取成功", users);
    }
    
    /**
     * 获取用户的粉丝列表
     */
    @Operation(summary = "获取粉丝列表", description = "获取用户的粉丝列表")
    @GetMapping("/{userId}/followers")
    public Result<PageResult<UserDTO>> getFollowers(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size) {
        PageResult<UserDTO> users = userService.getFollowers(userId, page, size);
        return Result.success("获取成功", users);
    }
    
    /**
     * 搜索用户
     */
    @Operation(summary = "搜索用户", description = "根据关键字搜索用户")
    @GetMapping("/search")
    public Result<PageResult<UserDTO>> searchUsers(
            @Parameter(description = "搜索关键字") @RequestParam(required = false) String keyword,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size) {
        PageResult<UserDTO> users = userService.searchUsers(keyword, page, size);
        return Result.success("搜索成功", users);
    }
    
    /**
     * 获取用户统计信息
     */
    @Operation(summary = "获取用户统计信息", description = "获取用户的帖子、评论、关注等统计数据")
    @GetMapping("/{userId}/stats")
    public Result<UserDTO.UserStats> getUserStats(@Parameter(description = "用户ID") @PathVariable Long userId) {
        UserDTO.UserStats stats = userService.getUserStats(userId);
        return Result.success("获取成功", stats);
    }


} 